package moplayer;

import generation.methods.BasicMarkov;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.sound.midi.InvalidMidiDataException;

import org.jfugue.Note;
import org.jfugue.Pattern;
import org.jfugue.Player;

public class MidiAnalyzer {

	public static HashMap<Integer, Integer> histograma = new HashMap<Integer, Integer>();
	public static HashMap<Integer, HashMap<Integer, Integer>> hashDHase = new HashMap<Integer, HashMap<Integer,Integer>>();
	public static int[][] noteCounter = new int[12][12];
	public static Vector<Integer> vector = new Vector<Integer>();

	public static void addNote(int noteValue){
		if(!histograma.containsKey(noteValue)){
			histograma.put(noteValue, 1);
		} else {
			int quant = histograma.get(noteValue);
			histograma.put(noteValue, quant + 1);
		}
	}

	public static void fillMatrix(int previous, int next){
		if(!hashDHase.containsKey(previous)){
			hashDHase.put(previous, new HashMap<Integer, Integer>());
		}
		if(!hashDHase.get(previous).containsKey(next)){
			hashDHase.get(previous).put(next, 1);
		} else {
			int temp = hashDHase.get(previous).get(next);
			hashDHase.get(previous).put(next, temp + 1);
		}
	}
	
	public static BasicMarkov analyzeMIDI(String midiFile, String voice) {
		
		BasicMarkov result = null;

		MidiLoader.loadMidi(midiFile, voice, vector);
		
		int[] notasX;
		for (int i = 0; i < vector.size() - 1; i++) {
			Integer previous = vector.get(i);
			Integer next = vector.get(i + 1);
			fillMatrix(previous, next);
		}
		Set<Integer> set = hashDHase.keySet();
		int i = 0;
		int j = 0;
		notasX = new int[set.size()];
		for (Integer key1 : set) {
			int[] notasY;
			System.out.println("i = " + i);
			HashMap<Integer, Integer> sub = hashDHase.get(key1);
			Set<Integer> subset = sub.keySet();
			notasY = new int[subset.size()];
			notasX[i] = key1;
			for (Integer key2 : subset) {
				System.out.println("j = " + j);
				Integer count = hashDHase.get(key1).get(key2);
				System.out.println("["+ key1 +"]"+ i +"["+ key2 +"]" + j + " = " + count);
				//System.out.println(teste++);
				notasY[j] = key2;
				j++;
				if(j == subset.size()){
					j = 0;
				}
			}
			i++;
			if(i == set.size()){
				i = 0;
			}
			for (int k = 0; k < notasY.length; k++) {
				System.out.print(notasY[k] + " ");
			}
		}
		
		for (int k = 0; k < notasX.length; k++) {
			System.out.print(notasX[k] + " ");
		}
		
		double[] soma = new double[notasX.length];
		i = 0;
		j = 0;
		for (Integer prev : set){
			HashMap<Integer, Integer> sub = hashDHase.get(prev);
			Set<Integer> subset = sub.keySet();
			for (Integer next : subset) {
				Integer count = hashDHase.get(prev).get(next);
				soma[i] = soma[i] + count;
			}
			i++;
		}

		double[][] markovMatrix = new double[notasX.length][notasX.length];
		// Andando de novo:
		i = 0;
		for (Integer prev : set){
			HashMap<Integer, Integer> sub = hashDHase.get(prev);
			Set<Integer> subset = sub.keySet();
			for (Integer next : subset) {
				Integer count = hashDHase.get(prev).get(next);
				int index1 = MoMidiUtils.getIndex(notasX, prev);
				int index2 = MoMidiUtils.getIndex(notasX, next);
				markovMatrix[index1][index2] = count/soma[i];
			}
			i++;
		}

		System.out.println();
		result = new BasicMarkov(notasX, markovMatrix);
		return result;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		analyzeMIDI("midis/meu.mid", "0");
	
	}
	
	

}
